window.onload = function() {
	var stocks = [];
	var content = document.getElementById("demo");
	$(window).scroll(function(event) {
		var scroll = $(window).scrollTop();
		document.getElementById("logo").style.top = scroll * 1.1 + 100;
		document.getElementById("buttonspawnconnect").style.top = scroll;
		if (document.getElementById("pop-upconnect") != null) {
			document.getElementById("pop-upconnect").style.top = 55 + scroll;
		}
	});
}
$(document).ready(function() {

});
function AJAX_JSON_Req(req, page) {
	employe = JSON.parse(req);
	display(employe, page);
}
function json() {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", './convertdb.php', true);
	var json = xmlhttp.responseText;
	document.getElementById("demo").innerHTML = json;
	xmlhttp.send();
}
function createInstance() {
	var req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("XHR not created");
			}
		}
	}
	return req;
}
function storing(data, element) {
	element.innerHTML = data;
}
function delayer() {
	window.location = "index.php";
}
function submitForm(page) {

	load = "chargement";
	if (page == undefined) {
		page = 'accueil';
	}
	if (page == "loadeleve") {
		$
		.ajax({
			url : 'loadeleve.php',
			type : 'GET'
		})
		.done(
				function(result) {

					submitForm('ModifEleve');
					StringJSON = JSON.parse(result);
					$("#table").waitUntilExists(
							function() {
								for (i = 0; i < StringJSON.length; i++) {
									$("#table")
									.append(
											'<tr><td contenteditable="true">'
											+ StringJSON[i].NOMELEVE
											+ '</td><td contenteditable="true">'
											+ StringJSON[i].IDCOURSE
											+ '</td><td contenteditable="true">'
											+ StringJSON[i].ANNEENAISSANCE
											+ '</td><td contenteditable="true">'
											+ StringJSON[i].ADRESSE
											+ '</td><td contenteditable="true">'
											+ StringJSON[i].CERTIFICAT
											+ '</td><td contenteditable="true">'
											+ StringJSON[i].AUTORISATIONP
											+ '</td><td class="button validation" onclick="CheckModification">Valider</td><br><td class="button validation" onclick="DeleteEleve">supprimer</td></tr>');

								}
							});
				});
		return;

	}
	display(load, page);

	$.post('convertdb.php', {
		page : page
	}, function(Json) {
		if (Json != undefined) {
			AJAX_JSON_Req(Json, page);
		}
	}, 'text');
}

function CheckModification()
{
	// CHECK DIFFERENT VALUES
	// APPEL AJAX ModificationEleve.php qui si les données sont differentes lance un update PDO

}

function DeleteEleve()
{
	// AJAX DeleteEleve.php
	// RETOUR AJAX -> Supprimer une ligne dans le tableau ou le display:none 
}
function display(employe, page) {

	if (employe == "chargement" && page != "Infoeleve") {

		var load = document.createElement("i");
		$(load).addClass("icon-spin6 animate-spin");
		document.getElementById("chargement").appendChild(load);
		load = "";
	} else {
		var out = "";
		var i;

		for (i = 0; i < employe.length; i++) {
			if (employe[i].TITRE == page) {
				out = employe[i].CORPS;
			}
		}

		if (page == "Infoeleve") {
			if (out != "") {

				var increment = parseInt($("#cpt").val()) + 1;
				if ($.isNumeric(increment) == false) {
					increment = 0;
				}

				$("#cpt").val(String(increment));
				var neweleve = document.createElement("div");
				neweleve.style.display = "none";
				neweleve.className = "test";
				neweleve.style.width = "100%";
				neweleve.style.height = "100%";
				neweleve.id = increment;
				neweleve.innerHTML = out;
				$(neweleve).find("input[name='nom']").attr("id",
						"nom_" + increment);
				$(neweleve).find("input[name='nom']").attr("name",
						"nom_" + increment);
				$(neweleve).find("label[for='nom']").attr("for",
						"nom_" + increment);
				$(neweleve).find("input[name='anneenaissance']").attr("id",
						"anneenaissance_" + increment);
				$(neweleve).find("input[name='anneenaissance']").attr("name",
						"anneenaissance_" + increment);
				$(neweleve).find("label[for='anneenaissance']").attr("for",
						"anneenaissance_" + increment);
				$(neweleve).find("input[name='autorisationP']").attr("id",
						"autorisationP_" + increment);
				$(neweleve).find("input[name='autorisationP']").attr("name",
						"autorisationP_" + increment);
				$(neweleve).find("label[for='autorisationP']").attr("for",
						"autorisationP_" + increment);
				$(neweleve).find("input[name='certificat']").attr("id",
						"certificat_" + increment);
				$(neweleve).find("input[name='certificat']").attr("name",
						"certificat_" + increment);
				$(neweleve).find("label[for='certificat']").attr("for",
						"certificat_" + increment);
				$(neweleve).find("textarea[name='adresse']").attr("id",
						"adresse_" + increment);
				$(neweleve).find("textarea[name='adresse']").attr("name",
						"adresse_" + increment);
				$(neweleve).find("label[for='adresse']").attr("for",
						"adresse_" + increment);
				document.getElementById("insceleve").appendChild(neweleve);
				/*
				 * var imgTrash = document.createElement("button");
				 * imgTrash.style.backgroundImage =
				 * "url('C:/wamp/www/svn/trunk/TPUSEP/media/img/delete-icon.png')";
				 * imgTrash.style.backgroundColor = 'white'; //imgTrash.onclick =
				 * ExecFctn.bind(imgTrash, 'imgTrash');
				 * imgTrash.setAttribute('cursor','pointer');
				 * imgTrash.style.border = 'none'; imgTrash.style.width= '33px';
				 * imgTrash.style.height= '32px'; //BOUTTON SUPPRIMER (Icon
				 * trashbin)
				 * 
				 * 
				 * document.getElementById(String(increment-1)).appendChild(imgTrash);
				 */
				$(neweleve).toggle('slide', {
					direction : 'up'
				}, 500);

				/*
				 * var btn = document.CreateElement("input"); // HOW TO CREATE A
				 * BUTTON btn.setAttribute("type", "button");
				 * btn.setAttribute("value", "Category");
				 * btn.style.backgroundImage = "url(path)"; // for image
				 * 
				 * var frm = document.forms[0]; frm.appendChild(btn);
				 */

			}
		} else {
			$("#features")
			.toggle(
					'slide',
					{
						direction : 'left'
					},
					500,
					function() {
						var icone = document
						.getElementsByClassName("icon-spin6 animate-spin")[0];
						icone.parentNode.removeChild(icone);

						document.getElementById("features").innerHTML = out;
						if (page == "acceuilAdmin") {
							recupecolevalid('validation');
						}
						if (page == "validationInscription") {
							recupecolevalid('validation');
						}
						if (page == "demandeAffiliation"||page=="attenteLicence"||page=="licenceEleve") {
							recupecolevalid(page);
						}
						$("#features").toggle('slide', {
							direction : 'right'
						}, 500);
					});
		}
	}
}

//function recupexistant(){
//$.post(
//'loadeleve.php',
//{
//page:page
//},
//function(Json){
//if(Json!=undefined){
//AJAX_JSON_Req(Json, page);}
//},
//'text'
//);
//}
function add() {
	var msg = document.getElementById('msg').value;
	var nom = document.getElementById('cache').value;
	$.post('send_msg.php', {
		utilisateur : nom,
		msg : msg
	}, function(data) {
		/*
		 * 
		 * if(data==success){ alert('message envoyÃ©'); } else{ alert('a problem
		 * has occured'); }
		 */
	}, 'text');
	document.getElementById('msg').value = "";

}
function inscription(type) {
	var req = createInstance();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				// storing(req.responseText, element);
				displayformulaire(req.responseText);

			} else {
				document.ajax.dyn.value = "Error: returned status code "
					+ req.status + " " + req.statusText;
			}
		}
	};
	req.open("GET", "./FormulaireInscription.php?var=" + type, true);
	req.send(null);
}
function displayformulaire(donnee) {
	document.getElementById("features").innerHTML = donnee;
}
function connexion() {
	var id = document.getElementById("identifiant").value;
	var pass = document.getElementById("password").value;
	$.post('connexion.php', {
		type : 'connexion',
		identifiant : id,
		mdp : pass
	}, function(data) {
		location.reload();
	}, 'text');
}

function deconnexion() {
	$.post('connexion.php', {
		type : 'deconnexion'
	}, function(data) {
		location.reload();
	}, 'text');
}
function recupecolevalid(type) {
	$.post('validationecole.php', {
		type: type+"recup"
	}, function(data) {
	}, 'text').done(function(parse) {
		validinscription(JSON.parse(parse), type)
	});
}
function validinscription(ecole, type) {
	if (ecole != undefined) {
		if (type!="attenteLicence") {
			if(type=="licenceEleve"){
				for (i = 0; i < ecole.length; i++) {
					$("#table")
					.append('<tr><td>'
							+ ecole[i].NOMELEVE
							+ '</td><td>'
							+ ecole[i].ANNEENAISSANCE
							+ '</td><td>'
							+ ecole[i].ADRESSE
							+ '</td><td>'
							+ ecole[i].CERTIFICAT
							+ '</td><td>'
							+ ecole[i].AUTORISATIONP
							+ '</td><td>'
							+ ecole[i].DROITIMAGE
							+ '</td><td>'
							+ ecole[i].EVACUATIONURGENCE
							+ '</td><td contenteditable=true>N° licence'
							+ '</td><td class="button '+type+'">Valider</td></tr>');
				}
			}else{
				for (i = 0; i < ecole.length; i++) {
					if(ecole[i].NOMECOLE != "admin"){
						$("#table")
						.append(
								'<tr><td>'
								+ ecole[i].NOMECOLE
								+ '</td><td>'
								+ ecole[i].VILLE
								+ '</td><td>'
								+ ecole[i].mail
								+ '</td><td class="button '+type+'">Valider</td></tr>');
					}
				}
				$("."+type).click(function() {
					valideffective(this,type);
				});
			}
		}
		else{

			for (i = 0; i < ecole.length; i++) {
				if(ecole[i].NOMECOLE != "admin"){
					$("#table")
					.append('<tr><td>'
							+ ecole[i].NOMECOLE
							+ '</td><td>'
							+ ecole[i].VILLE
							+ '</td><td>'
							+ ecole[i].mail
							+ '</td><td contenteditable=true>N° licence'
							+ '</td><td class="button '+type+'">selectionner</td></tr>');

				}
			}

			$('td').focus(function() {
				if($(this).html()=="N° licence"){
					$(this).empty();
					// how to set focus without triggering focus
				}
			});
			$('td').blur(function() {
				if($(this).html()==""){
					$(this).html("N° licence");
				}
			});
			$("."+type).click(function() {
				SelectEcole(this)
				submitForm("licenceEleve");
			});
		}

	}
}
//Recupération des écoles en attente de validation de la part d'un admin
function valideffective(ecole, type) {
//	$.post('validationecole.php', {
//	type : 'validation',
//	nom : $(ecole).parent().children("td").eq(0).html(),
//	ville : $(ecole).parent().children("td").eq(1).html()
//	}, function(data) {
//	$(ecole).parent().toggle(500);

//	}, ' json');
	var vdata={"type":type,"nom":$(ecole).parent().children("td").eq(0).html(),"ville":$(ecole).parent().children("td").eq(1).html()};
	$.ajax({
		type: "POST",
		url: 'validationecole.php',
		data: vdata,
		success: $(ecole).parent().toggle(500),   //fait disparaitre la ligne
		dataType: "json",
		encode:true
	});
}
function SelectEcole(ecole){
	document.cookie="nom="+$(ecole).parent().children("td").eq(0).html();
	document.cookie="ville="+$(ecole).parent().children("td").eq(1).html();
}
